package database.configuration;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import security.AES256;

import database.hibernate.HibernateUtil;
import database.bean.AuthCardData;
import database.bean.User;

import enumTypes.Civility;

/**
 * Classe de test pour la base de données.
 * @author Guillaume Pocas & Jean-Fraçois Augé
 */
public class DatabaseConfiguration {
	
	/**
	 * Données de tests pour la base de données.
	 */
	public static void new_build() {
		// generate keyStore
		final String ksPath = "C:\\Users\\B182JS\\workspace\\projet_SmartCard\\bin\\aesKeyStore.keystore";
		final char[] ksPassword = {'t','o','t','o'};
		final char[] keyPassword = {'t','o','t','o'};
		String alias = "aesKey";
		
		AES256.createAESKeyStore(ksPath, ksPassword, keyPassword, alias);
		
		//vidage des tables
		
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		
		List<User> list1 = session.createCriteria(User.class).list();
		//User user_toto = (User) session.createCriteria(User.class).add(Restrictions.like("name", "toto")).list().get(0);

		System.out.println("\tusers:");
		for(User u : list1){
			System.out.println(u);
			session.delete(u);
		}
		
		List<AuthCardData> list2 = session.createCriteria(AuthCardData.class).list();
		System.out.println("\tcards:");
		for(AuthCardData c : list2){
			System.out.println(c);
			session.delete(c);
		}
		
		tx.commit();
		
		//ajout des données
		
		//users
		List<User> resultats = session.createCriteria(User.class).list();
		boolean exist = false;
		
		for(User usr : resultats){
			if(usr.getUser_lastname().equalsIgnoreCase("admin"))	exist = true;
		}
		
		if(!exist) {
			tx = session.beginTransaction();
			
			User userAdmin = new User("admin", "a", Civility.Monsieur);
			session.save(userAdmin);

			tx.commit();
		}
		
		User user = (User) session.createCriteria(User.class).list().get(0);
		
		//cards
		tx = session.beginTransaction(); //3207154C0103296613
		AuthCardData card1 = new AuthCardData("1", "1", "sel_pin1", "pin21", "1", "otp1", "password1");
		session.save(card1);
		AuthCardData card2 = new AuthCardData("2", "2", "sel_pin2", "pin22", "1", "otp2", "password2");
		session.save(card2);
		AuthCardData card3 = new AuthCardData("3", "3", "sel_pin3", "pin23", "1", "otp3", "password3");
		session.save(card3);
		AuthCardData card4 = new AuthCardData("4", "4", "sel_pin4", "pin24", "1", "otp4", "password4");
		session.save(card4);
		
		card1.setUser(user);
		user.setCard(card1);
		
		session.save(user);
		session.save(card1);
		
		tx.commit();
		
		System.out.println("Test nom du user qui a la card1 : "+
		card1.getUser().getUser_lastname());
		
		//fermeture de session
		session.close();
		
	}
	
	public static void main(String[] args) {
		DatabaseConfiguration.new_build();
	}

}
